load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_library", "rust_test")
load("//bazel:canisters.bzl", "rust_canister")
load("//bazel:defs.bzl", "rust_ic_test_suite")

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "helper_contracts",
    srcs = [
        "DepositHelperWithSubaccount.sol",
        "ERC20DepositHelper.sol",
        "EthDepositHelper.sol",
    ],
    visibility = ["//visibility:public"],
)

rust_library(
    name = "minter",
    srcs = glob(
        ["src/**/*.rs"],
        exclude = [
            "src/main.rs",
            "src/dashboard.rs",
            "src/dashboard/tests.rs",
        ],
    ),
    crate_name = "ic_cketh_minter",
    proc_macro_deps = [
        # Keep sorted.
        "@crate_index//:strum_macros",
    ],
    version = "0.1.0",
    deps = [
        # Keep sorted.
        "//packages/ic-ethereum-types",
        "//packages/ic-secp256k1",
        "//packages/icrc-cbor:icrc-cbor_u256",
        "//packages/icrc-ledger-client-cdk:icrc_ledger_client_cdk",
        "//packages/icrc-ledger-types:icrc_ledger_types",
        "//rs/phantom_newtype",
        "//rs/types/management_canister_types",
        "//rs/utils/ensure",
        "@crate_index//:candid",
        "@crate_index//:ethnum",
        "@crate_index//:evm_rpc_client",
        "@crate_index//:evm_rpc_types",
        "@crate_index//:futures",
        "@crate_index//:hex",
        "@crate_index//:hex-literal",
        "@crate_index//:ic-canister-log",
        "@crate_index//:ic-canister-runtime",
        "@crate_index//:ic-cdk",
        "@crate_index//:ic-cdk-timers",
        "@crate_index//:ic-metrics-encoder",
        "@crate_index//:ic-sha3",
        "@crate_index//:ic-stable-structures",
        "@crate_index//:minicbor",
        "@crate_index//:num-bigint",
        "@crate_index//:num-traits",
        "@crate_index//:rlp",
        "@crate_index//:scopeguard",
        "@crate_index//:serde",
        "@crate_index//:serde_bytes",
        "@crate_index//:serde_json",
        "@crate_index//:strum",
        "@crate_index//:thiserror",
        "@crate_index//:thousands",
    ],
)

rust_doc(
    name = "doc",
    crate = ":minter",
)

rust_doc_test(
    name = "doc_test",
    crate = ":minter",
)

rust_test(
    name = "lib_tests",
    crate = ":minter",
    data = [
        "test_resources/mainnet_events.gz",
        "test_resources/sepolia_events.gz",
    ],
    env = {
        "CARGO_MANIFEST_DIR": "rs/ethereum/cketh/minter",
    },
    deps = [
        # Keep sorted.
        ":minter",
        "//rs/crypto/test_utils/reproducible_rng",
        "@crate_index//:assert_matches",
        "@crate_index//:candid_parser",
        "@crate_index//:ethers-core",
        "@crate_index//:flate2",
        "@crate_index//:ic-agent",
        "@crate_index//:maplit",
        "@crate_index//:proptest",
        "@crate_index//:rand",
        "@crate_index//:tempfile",
        "@crate_index//:tokio",
    ],
)

rust_test(
    name = "unit_tests",
    crate = ":_wasm_cketh_minter",
    data = [":cketh_minter.did"],
    env = {
        "CARGO_MANIFEST_DIR": "rs/ethereum/cketh/minter",
    },
    deps = [
        # Keep sorted.
        ":minter",
        "@crate_index//:candid_parser",
        "@crate_index//:maplit",
        "@crate_index//:scraper",
    ],
)

[rust_canister(
    name = "cketh_minter" + target_suffix,
    srcs = glob(["src/**/*.rs"]),
    compile_data = [
        "templates/dashboard.html",
        "templates/pagination.html",
        "templates/principal_to_bytes.js",
    ],
    crate_features = features,
    crate_name = "ic_cketh_minter_canister",
    proc_macro_deps = [
        # Keep sorted.
    ],
    service_file = "cketh_minter.did",
    deps = [
        # Keep sorted.
        ":minter",
        "//packages/ic-ethereum-types",
        "//packages/ic-http-types",
        "//packages/ic-secp256k1",
        "//packages/icrc-ledger-client-cdk:icrc_ledger_client_cdk",
        "//packages/icrc-ledger-types:icrc_ledger_types",
        "@crate_index//:askama",
        "@crate_index//:candid",
        "@crate_index//:futures",
        "@crate_index//:hex",
        "@crate_index//:ic-canister-log",
        "@crate_index//:ic-cdk",
        "@crate_index//:ic-cdk-timers",
        "@crate_index//:ic-metrics-encoder",
        "@crate_index//:num-traits",
        "@crate_index//:serde_bytes",
        "@crate_index//:time",
    ],
) for (target_suffix, features) in [
    ("", []),
    (
        "_debug",
        ["debug_checks"],
    ),
]]

rust_binary(
    name = "principal_to_hex",
    srcs = ["bin/principal_to_hex.rs"],
    deps = [
        # Keep sorted.
        "@crate_index//:candid",
        "@crate_index//:hex",
    ],
)

rust_ic_test_suite(
    name = "integration_tests",
    # the test sometimes times out on CI with default timeout
    # of "moderate" (5 minutes) - 2025-07-11
    timeout = "long",
    srcs = glob(["tests/**/*.rs"]),
    data = [
        ":cketh_minter_debug.wasm.gz",
        "//rs/ethereum/ledger-suite-orchestrator:ledger_suite_orchestrator_canister.wasm.gz",
        "//rs/ledger_suite/icrc1/archive:archive_canister_u256.wasm.gz",
        "//rs/ledger_suite/icrc1/index-ng:index_ng_canister_u256.wasm.gz",
        "//rs/ledger_suite/icrc1/ledger:ledger_canister_u256.wasm.gz",
        "@evm_rpc.wasm.gz//file",
    ],
    env = {
        "CARGO_MANIFEST_DIR": "rs/ethereum/cketh/minter",
        "CKETH_MINTER_WASM_PATH": "$(rootpath :cketh_minter_debug.wasm.gz)",
        "LEDGER_SUITE_ORCHESTRATOR_WASM_PATH": "$(rootpath //rs/ethereum/ledger-suite-orchestrator:ledger_suite_orchestrator_canister.wasm.gz)",
        "LEDGER_CANISTER_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/ledger:ledger_canister_u256.wasm.gz)",
        "INDEX_CANISTER_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/index-ng:index_ng_canister_u256.wasm.gz)",
        "LEDGER_ARCHIVE_NODE_CANISTER_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/archive:archive_canister_u256.wasm.gz)",
        "EVM_RPC_CANISTER_WASM_PATH": "$(rootpath @evm_rpc.wasm.gz//file)",
    },
    proc_macro_deps = [],
    deps = [
        # Keep sorted.
        ":minter",
        "//packages/ic-ethereum-types",
        "//packages/icrc-ledger-types:icrc_ledger_types",
        "//rs/ethereum/cketh/test_utils",
        "//rs/ethereum/ledger-suite-orchestrator/test_utils",
        "//rs/state_machine_tests",
        "//rs/types/base_types",
        "//rs/types/management_canister_types",
        "@crate_index//:assert_matches",
        "@crate_index//:candid",
        "@crate_index//:ethers-core",
        "@crate_index//:hex",
        "@crate_index//:num-bigint",
        "@crate_index//:num-traits",
        "@crate_index//:serde",
        "@crate_index//:serde_bytes",
        "@crate_index//:serde_json",
    ],
)
